//
//  LoginViewController.m
//  VICKY ZONE
//
//  Created by sunx on 15/3/19.
//  Copyright (c) 2015年 sunx. All rights reserved.
//

#import "LoginViewController.h"
#import "IQKeyboardManager.h"
#import "IQKeyboardReturnKeyHandler.h"
#import "IQUIView+IQKeyboardToolbar.h"
#import "SVProgressHUD.h"
#import "MainViewController.h"

#define SERVE @"http://www.samoyun.com/controller/loginController.php"

@interface LoginViewController ()
@property(nonatomic,strong) NSDictionary *infoDictionary;
@property(nonatomic,strong) NSUserDefaults *userDefault;
@property(nonatomic) int integer;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.username = @"";
}

- (void)viewWillAppear:(BOOL)animated {
    [self guidePageAutoJudge];
}

- (void)viewDidAppear:(BOOL)animated {
    self.username = (NSString *)[self.userDefault valueForKey:@"username"];
    NSLog(@"username : %@",self.username);
    self.userField.text = @"";
    self.pwdField.text = @"";
    
    if(self.username.length != 0) {
        [self backToMain];
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [SVProgressHUD dismiss];
}


- (void)guidePageAutoJudge {
    self.userDefault = [NSUserDefaults standardUserDefaults];
    
    self.infoDictionary = [[NSBundle mainBundle] infoDictionary];
    
    NSString *app_Version = [self.infoDictionary objectForKey:@"CFBundleShortVersionString"];
    
    int status = (int)[self.userDefault integerForKey:app_Version];
    NSLog(@"log status:%d",status);
    
    if(status == 0) {
        [self showGuidePage];
    }
}
#pragma mark - login action

-(IBAction)login:(id)sender {
    if([self.userField.text isEqualToString:@""] || [self.pwdField.text isEqualToString:@""]) {
        NSLog(@"邮箱或密码为空");
    }else {
        [self post];
    }
}
- (void)post {
    [SVProgressHUD show];
    NSString *password = [MD5Tool md5:self.pwdField.text];
    NSString *email = self.userField.text;
    [NSURLSession POST:SERVE pararms:@{@"email" : email,@"pwd" : password} completed:^(NSData *data) {
        if(data != nil) {
            
            NSString *username = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            
            //NSLog(@"post back is :%@;",username);
            if(![username isEqual:@""]){
                NSLog(@"密码正确");
                self.username = username;
                [self.userDefault setObject:username forKey:@"username"];
                [self backToMain];
            }else {
                NSLog(@"密码错误");
                [SVProgressHUD dismiss];
                [self noUserError];
            }
        } else {
            [SVProgressHUD dismiss];
        }
    } failed:^(NSError *error){
        NSLog(@"%@",error);
        NSLog(@"error");
        [SVProgressHUD dismiss];
    }];
}

- (void)backToMain {
    [self dismissViewControllerAnimated:YES completion:^{
        NSLog(@"dismiss complete");
    }];
}
- (void)noUserError {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ERROR" message:@"用户名密码错误" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
    NSLog(@"...");
    [alert show];
    [SVProgressHUD dismiss];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - guide page
- (void)showGuidePage {
    EAIntroPage *page1 = [EAIntroPage page];
    page1.bgImage = [UIImage imageNamed:@"1.jpg"];
    
    EAIntroPage *page2 = [EAIntroPage page];
    page2.bgImage = [UIImage imageNamed:@"2.jpg"];
    
    EAIntroPage *page3 = [EAIntroPage page];
    page3.bgImage = [UIImage imageNamed:@"3.jpg"];
    
    EAIntroPage *page4 = [EAIntroPage page];
    page4.bgImage = [UIImage imageNamed:@"4.jpg"];
    
    EAIntroView *intro = [[EAIntroView alloc] initWithFrame:self.view.bounds andPages:@[page1,page2,page3,page4]];
    
    [intro setDelegate:self];
    [intro showInView:self.view animateDuration:0.0];
    
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    
    NSString *app_Version = [self.infoDictionary objectForKey:@"CFBundleShortVersionString"];
    
    [userDefault setInteger:1 forKey:app_Version];
}
- (void)introDidFinish {
    NSLog(@"guide finish...");
}
- (IBAction)backToHere:(UIStoryboardSegue *)sender {
}
@end
